import common from './common';


export default {
    /*
     *控制下拉刷新
     */
    switchPullToRefresh(currentWebview, support) {
        currentWebview.setStyle({
            pullToRefresh: {
                support,
                style: plus.os.name === 'Android' ? 'circle' : 'default'
            }
        });
    },
    /**
     * 状态栏
     */
    statusBarShow(that, scrollTop) {
        that.statusBar = scrollTop > 0 ? true : false;
    },
    /*获取手机内部总的存储空间*/
    getTotalInternalMemorySize() {
        console.log(plus.os.name)
        if (plus.os.name == "Android") {
            console.log('Android路线');
            var internalMemSize = 0;
            var romFreeSpace = 0;
            var environment = plus.android.importClass("android.os.Environment");
            var statFs = plus.android.importClass("android.os.StatFs");
            var files = plus.android.importClass("java.io.File");
            var Files = environment.getDataDirectory();
            var StatFs = new statFs(Files.getPath());
            var blockSize = parseFloat(StatFs.getBlockSize());
            var blockCount = parseFloat(StatFs.getBlockCount());
            internalMemSize = blockSize * blockCount;
            romFreeSpace = Files.getFreeSpace(); //获取内存可用剩余空间
            romFreeSpace = (romFreeSpace / (1024 * 1024 * 1024)).toFixed(2) + "GB";
            return romFreeSpace;
        }
        if (plus.os.name == "iOS") {
            console.log('iOS路线');
            var BundleClass = plus.ios.importClass("NSBundle");
            var BundleObj = BundleClass.mainBundle();
            var filenamagerobj = plus.ios.newObject("NSFileManager");
            var FileAttr = plus.ios.invoke(filenamagerobj, "attributesOfFileSystemForPath:error:", BundleObj.bundlePath(),
                null);
            var freeSpace = plus.ios.invoke(FileAttr, "objectForKey:", "NSFileSystemFreeSize");
            var numberFormatterObj = plus.ios.newObject("NSNumberFormatter");
            var FreeSpaceStr = plus.ios.invoke(numberFormatterObj, "stringFromNumber:", freeSpace);
            var freeSpace = FreeSpaceStr / 1024 / 1024 / 1024;
            return freeSpace.toFixed(2) + "GB";
        }
    },
    /*
     *监听网络变化事件
     */
    onNetChange() {
        let nt = plus.networkinfo.getCurrentType();
        switch (nt) {
            case plus.networkinfo.CONNECTION_ETHERNET:
            case plus.networkinfo.CONNECTION_WIFI:
                //mui.alert("使用wifi中");
                return 0
                break;
            case plus.networkinfo.CONNECTION_CELL2G:
            case plus.networkinfo.CONNECTION_CELL3G:
            case plus.networkinfo.CONNECTION_CELL4G:
                //mui.alert("使用数据流量中");
                return 1
                break;
            default:
                mui.alert("当前没有网络");
                return 2
                break;
        }
    },
    /*
     *更新版本
     */
    version: {
        init(callback) {
            console.log('version init');
            /*
            alert(plus.runtime.appid);
            alert(plus.runtime.version);
            */
            console.log(plus.runtime.appid)
            if (callback && typeof callback == 'function') {
                callback();
            }
        },
        //获取版本号
        getProperty() {
            if (!plus) {
                return false
            }
            plus.runtime.getProperty(plus.runtime.appid, function(inf) {
                var wgtVer = inf.version;
                console.log("当前应用版本：" + wgtVer);
                return wgtVer;
            });
        },
        //检查新版本
        checkUpdate() {
            plus.nativeUI.showWaiting("检测更新...");
        }
    },
    /*
     *定位
     * */
    geolocation(callback) {

        if (plus.storage.getItem('addressInfo')) {
            callback(JSON.parse(plus.storage.getItem('addressInfo')));
        } else {
            plus.geolocation.getCurrentPosition(function(e) {
                console.log('获取定位成功',JSON.stringify(e));
                plus.storage.setItem('addressInfo', JSON.stringify(e));
                callback(e);
            }, function(e) {
                console.log('获取定位失败原因');
                console.log(e.message);
                uni.showToast({
                    title: '获取定位失败' + e.message,
                    mask: false,
                    icon: 'none',
                    duration: 1500
                });
                callback({});
            })
        }
    },
    //获取支付通道
    getPayChanel(payname, callback) {
        plus.payment.getChannels(function(channels) {
            let istrue = false;
            for (var i in channels) {
                if (channels[i].id == payname) {
                    istrue = true;
                    callback && callback();
                }
            }
            if (!istrue) {
                common.showTip(`当前不支持${payname}支付`)
            }
        }, function(e) {
            common.showTip("获取支付通道失败：" + e.message)
        });
    },
    //获取通讯录联系人
    getContacts(that) {
        if(that.userInfo && that.userInfo.is_get_contacts == 0){
            /*读取联系人*/
            plus.contacts.getAddressBook(
                plus.contacts.ADDRESSBOOK_PHONE,
                function(addressbook) {
                    addressbook.find(
                        '',
                        function(contacts) {
                            let list = [];
                            if (contacts.length) {
                                for (let i = 0, l = contacts.length; i < l; i++) {
                                    let displayName = contacts[i].displayName;
                                    let phoneNumbers = contacts[i].phoneNumbers;
                                    let tel = [];
                                    phoneNumbers.forEach(item => {
                                        tel.push(item.value || '');
                                    })
                                    list.push({
                                        name: displayName,
                                        phone: tel.join('-')
                                    })
                                    // phoneNumbers[0].value; // 手机号
                                    /*
                                    console.log("id: " + contacts[i].id);
                                    console.log("displayName: " + contacts[i].displayName);
                                    console.log("name: " + JSON.stringify(contacts[i].name));
                                    console.log("nickname: " + JSON.stringify(contacts[i].nickname));
                                    console.log("phoneNumbers: " + JSON.stringify(contacts[i].phoneNumbers));
                                    console.log("emails: " + JSON.stringify(contacts[i].emails));*/
                                }

                                console.log("list: " + JSON.stringify(list));  
                            }
                        },
                        function() {}, {
                            multiple: true
                        },
                    );
                },
                function(e) {
                    console.log('Get address book failed: ' + e.message);
                }
            );
    
        }
    }
}
